import Mock from 'mockjs'
import { param2Obj } from '../utils'

// 热门搜索关键词
const hotSearches = [
  '报销申请',
  '物资申领',
  '工资查询',
  '请假申请',
  '资产维修',
  '预算查询'
]

// 搜索结果
const searchResults = Mock.mock({
  'finance|15': [{
    'id|+1': 1,
    'name': '@ctitle(4, 10)',
    'description': '@csentence(10, 20)',
    'type': 'finance',
    'icon': 'balance-o',
    'path': '/finance/@word(5, 8)'
  }],
  'material|15': [{
    'id|+1': 100,
    'name': '@ctitle(4, 10)',
    'description': '@csentence(10, 20)',
    'type': 'material',
    'icon': 'shopping-cart-o',
    'path': '/material/@word(5, 8)'
  }],
  'asset|15': [{
    'id|+1': 200,
    'name': '@ctitle(4, 10)',
    'description': '@csentence(10, 20)',
    'type': 'asset',
    'icon': 'desktop-o',
    'path': '/asset/@word(5, 8)'
  }],
  'profile|15': [{
    'id|+1': 300,
    'name': '@ctitle(4, 10)',
    'description': '@csentence(10, 20)',
    'type': 'profile',
    'icon': 'user-o',
    'path': '/profile/@word(5, 8)'
  }]
})

// 搜索历史存储
let searchHistory = []

export default {
  // 全局搜索
  search: config => {
    const { keyword, types, page = 1, limit = 10 } = param2Obj(config.url)
    
    // 如果没有关键词，返回空结果
    if (!keyword || keyword.trim() === '') {
      return {
        code: 200,
        data: {
          total: 0,
          items: [],
          page: Number(page),
          limit: Number(limit)
        }
      }
    }
    
    // 过滤搜索结果
    let mockList = []
    const typeList = types ? types.split(',') : ['finance', 'material', 'asset', 'profile']
    
    typeList.forEach(type => {
      if (searchResults[type]) {
        const filtered = searchResults[type].filter(item => {
          return item.name.includes(keyword) || item.description.includes(keyword)
        })
        mockList = [...mockList, ...filtered]
      }
    })
    
    // 分页
    const pageList = mockList.filter((item, index) => {
      return index < limit * page && index >= limit * (page - 1)
    })
    
    return {
      code: 200,
      data: {
        total: mockList.length,
        items: pageList,
        page: Number(page),
        limit: Number(limit)
      }
    }
  },
  
  // 获取搜索建议
  getSuggestions: config => {
    const { keyword } = param2Obj(config.url)
    
    if (!keyword || keyword.trim() === '') {
      return {
        code: 200,
        data: []
      }
    }
    
    // 模拟搜索建议
    const allItems = [
      ...searchResults.finance,
      ...searchResults.material,
      ...searchResults.asset,
      ...searchResults.profile
    ]
    
    const suggestions = allItems
      .filter(item => item.name.includes(keyword))
      .slice(0, 5)
      .map(item => ({
        value: item.name,
        type: item.type,
        icon: item.icon
      }))
    
    return {
      code: 200,
      data: suggestions
    }
  },
  
  // 获取热门搜索
  getHotSearches: () => {
    return {
      code: 200,
      data: hotSearches
    }
  },
  
  // 记录搜索历史
  recordSearch: config => {
    const { keyword } = JSON.parse(config.body)
    
    if (keyword && keyword.trim() !== '') {
      // 如果已存在，先移除
      const index = searchHistory.indexOf(keyword)
      if (index !== -1) {
        searchHistory.splice(index, 1)
      }
      
      // 添加到开头
      searchHistory.unshift(keyword)
      
      // 限制最大数量
      if (searchHistory.length > 10) {
        searchHistory.pop()
      }
    }
    
    return {
      code: 200,
      data: 'success'
    }
  },
  
  // 获取搜索历史
  getSearchHistory: () => {
    return {
      code: 200,
      data: searchHistory
    }
  },
  
  // 清除搜索历史
  clearSearchHistory: () => {
    searchHistory = []
    
    return {
      code: 200,
      data: 'success'
    }
  }
} 